package com.gz.mylife.im;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;

import com.gz.mylife.Define;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.listener.UploadPictureListener;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;

public class ImageRequest {

	private Activity mActivity;
	
	private String mUploadUrl = Constants.URL_UPLOAD_PICTURE;
	
	public void setUploadUrl(String mUploadUrl) {
		this.mUploadUrl = mUploadUrl;
	}

	public ImageRequest(Activity activity) {
		mActivity = activity;
	}
	
	private UploadListener mListener;

	/**
	 * 上传消息和聊天室图片接口
	 * @param imageFile
	 * @param api
	 */
	public void uploadMsgImage(File imageFile,UploadListener listener){
		mListener = listener;
		if (!Tools.hasInternetConnection(mActivity)) {
			listener.failure(-1, Define.MSG_NO_NETWORK);
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		RequestParams params = new RequestParams();
		try {
			//1 图像  2face
			params.put("image", imageFile);
		} catch (FileNotFoundException e) {
			listener.failure(0, "文件不存在");
			return;
		}
		SingtonAsyncHttpClient.getInstance().post(mUploadUrl, params, new UploadPictureListener() {
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				mListener.start();
			}
			
			@Override
			public void onResponseFailed() {
				// TODO Auto-generated method stub
				mListener.failure(-1, Define.MSG_OPERATE_FAILED_RETRY);
			}
			
			@Override
			public void onOffline() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onNoNetWork() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onResponseSuccess(String image, int width, int height) {
				// TODO Auto-generated method stub
				Photo p = new Photo();
				p.setUid(IAuth.getInstance().loadUserInfo().getUid());
				p.setUrl(image);
				p.setWidth(width);
				p.setHeight(height);
				mListener.success(p);
			}
		});
//		mClient.post(mUploadUrl, params, new JsonHttpResponseHandler() {
//			@Override
//			public void onStart() {
//				mListener.start();
//			}
//
//			@Override
//			public void onSuccess(int statusCode, Header[] headers,	JSONObject response) {
//				try {
//					int ret = response.getInt("ret");
//					String msg = response.getString("msg");
//					if (ret == 0) {
//						JSONObject object = response.getJSONObject("result");
//						String image = object.getString("image");
//						int width = object.getInt("width");
//						int height = object.getInt("height");
//						Photo p = new Photo();
//						p.setUid(IAuth.getInstance().loadUserInfo().getUid());
//						p.setUrl(image);
//						p.setWidth(width);
//						p.setHeight(height);
//						mListener.success(p);
//					}  else {
//						mListener.failure(ret, msg);
//					}
//				} catch (JSONException e) {
//					mListener.failure(-1, "数据解析失败");
//				}
//			}
//
//			@Override
//			public void onFailure(Throwable error, String content) {
//				mListener.failure(-1, Define.MSG_CONNECT_ERROR);
//			}
//		});
	}
	
	/**
	 * 上传照片墙图片接口
	 * @param imageFile
	 * @param api
	 */
	public void upload(File imageFile,UploadListener listener){
		mListener = listener;
		if (!Tools.hasInternetConnection(mActivity)) {
			listener.failure(-1,Define.MSG_NO_NETWORK);
			return;
		}
		
		SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
		RequestParams params = new RequestParams();
		try {
			//1 图像  2face
			params.put("image", imageFile);
		} catch (FileNotFoundException e) {
			listener.failure(0, "文件不存在");
			return;
		}

		SingtonAsyncHttpClient.getInstance().post(mUploadUrl, params, new UploadPictureListener() {
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				mListener.start();
			}
			
			@Override
			public void onResponseFailed() {
				// TODO Auto-generated method stub
				mListener.failure(-1, Define.MSG_OPERATE_FAILED_RETRY);
			}
			
			@Override
			public void onOffline() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onNoNetWork() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onResponseSuccess(String image, int width, int height) {
				// TODO Auto-generated method stub
				Photo p = new Photo();
				p.setUid(IAuth.getInstance().loadUserInfo().getUid());
				p.setUrl(image);
				p.setWidth(width);
				p.setHeight(height);
				getImageId(p);
			}
		});
//		SingtonAsyncHttpClient.getInstance().post(mUploadUrl, params, new JsonHttpResponseHandler() {
//			@Override
//			public void onStart() {
//				mListener.start();
//			}
//
//			@Override
//			public void onSuccess(int statusCode, Header[] headers,	JSONObject response) {
//				try {
//					int ret = response.getInt("ret");
//					String msg = response.getString("msg");
//					if (ret == 0) {
//						JSONObject object = response.getJSONObject("result");
//						String image = object.getString("image");
//						int width = object.getInt("width");
//						int height = object.getInt("height");
//						Photo p = new Photo();
//						p.setUid(IAuth.getInstance().loadUserInfo().getUid());
//						p.setUrl(image);
//						p.setWidth(width);
//						p.setHeight(height);
//						getImageId(p);
//					}  else {
//						mListener.failure(ret, msg);
//					}
//				} catch (JSONException e) {
//					mListener.failure(-1, "数据解析失败");
//				}
//			}
//
//			@Override
//			public void onFailure(Throwable error, String content) {
//				mListener.failure(-1, Define.MSG_CONNECT_ERROR);
//			}
//		});
	}
	
	private void getImageId(final Photo photo){
		if (!Tools.hasInternetConnection(mActivity)) {
			mListener.failure(-1,Define.MSG_NO_NETWORK);
			return;
		}
		
		RequestParams params = new RequestParams();
		params.put("image", photo.getUrl());
		params.put("width", photo.getWidth()+"");
		params.put("height", photo.getHeight()+"");
		SingtonAsyncHttpClient.getInstance().post(Constants.URL_GET_PHOTO_ID, params, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
				try {
					int ret = response.getInt("ret");
					String msg = response.getString("msg");
					if (ret == 0) {
						JSONObject object = response.getJSONObject("result");
						String url = object.getString("url");
						int id = object.getInt("id");
						photo.setUrl(url);
						photo.setPhotoId(id);
						mListener.success(photo);
					} else {
						mListener.failure(ret, msg);
					}
				} catch (JSONException e) {
					mListener.failure(-1, "数据解析失败");
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				mListener.failure(-1, Define.MSG_OPERATE_FAILED_RETRY);
			}
		});
	}
	
	
	public static interface UploadListener{
		public void start();
		public void success(Photo photo);
		public void failure(int errorCode,String errorMsg);
	}
}
